<?php
namespace app\index\model;

use think\Model;
use think\Request;

class Cron extends Model
{

    protected $connection = ['prefix' => 'acc_dragon_'];

    //重写父类的初始化方法initialize()
    protected function initialize()
    {

        //继承父类initialize()
        parent::initialize();

        //初始化数据表名称
        $this->table = 'acc_dragon_cron';
    }
    /**
     * 列表(主键列表，条件数组，闭包查询)
     *
     * @param    array $param 查询条件
     * @return   void
     */
    public function doList(Request $request)
    {
        //返回订单列表
        //
        $param = $request->param();

        $page = $param['page'];

        $start = $param['start'];
        $limit = $param['limit'];

        $offset = ($page - 1) * $limit;

        //日志类型及查询日期
        //
        $type = array_key_exists("type", $param) ? $param['type'] : null;
        $date = array_key_exists("date", $param) ? $param['date'] : null;

        // 匹配条件
        //
        if ((!empty($type) || '0' == $type) && (!empty($date))) {

            $where = "DATEDIFF(`create_time`,'{$date}') = 0 AND `type` = '{$type}'";

        } else if (!empty($type) || '0' == $type) {

            $where = "`type` = '{$type}'";

        } else if (!empty($date)) {

            $where = "DATEDIFF(`create_time`,'{$date}') = 0";

        } else {

            $date = date('Y-m-d');

            //默认参数(当天)配合数据仓库字段加载
            $where = "DATEDIFF(`create_time`,'{$date}') = 0 AND `type` = '0'";
        }

        try {

            //查询数据
            $result = self::all(function ($query) use ($offset, $limit, $where) {
                $query->where($where)
                    ->limit($offset, $limit)
                    ->order('create_time', 'desc');
            });

            $rows = [];

            foreach ($result as $data) {

                $item = $data->getData();
                array_push($rows, $item);
            }

            //返回结果
            //
            $res = array();

            //查询数据总量
            $total = self::where($where)->count();

            if ($total > 0) {
                $res['rows'] = $rows;
                $res['total'] = $total;
            } else {
                $res['total'] = 0;
                $res['rows'] = array();
            }

            //请求类型返回
            return $res;

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
}
